<template>
  <el-form ref="deptForm" :rules="rules" :model="form" label-width="120px">
    <el-form-item label="部门名称" prop="name">
      <el-input
        v-model="form.name"
        style="width: 80%"
        placeholder="1-50个字符"
      />
    </el-form-item>
    <el-form-item label="部门编码" prop="code">
      <el-input
        v-model="form.code"
        style="width: 80%"
        placeholder="1-50个字符"
      />
    </el-form-item>
    <el-form-item label="部门负责人" prop="manager">
      <el-select v-model="form.manager" style="width: 80%" placeholder="请选择">
        <el-option
          v-for="item in list"
          :key="item.id"
          :value="item.username"
          :label="item.username"
        />
      </el-select>
    </el-form-item>
    <el-form-item label="部门介绍" prop="introduce">
      <el-input
        v-model="form.introduce"
        style="width: 80%"
        placeholder="1-300个字符"
        type="textarea"
        :rows="3"
      />
    </el-form-item>
    <el-form-item>
      <el-button type="primary" size="small" @click="hSubmit">确定</el-button>
      <el-button size="small" @click.native="$emit('close')">取消</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import { getEmployee } from '@/api/employee'
import {
  addDepartments,
  getDepartDetail,
  updateDepartments
} from '@/api/departments'
export default {
  props: {
    pid: {
      required: true,
      type: String
    },
    isEdit: {
      required: true,
      type: Boolean
    },
    originList: {
      required: true,
      type: Array
    }
  },
  data() {
    const validCode = (rule, value, callback) => {
      let existCodeList = this.originList.map((item) => item.code)
      if (this.isEdit) {
        existCodeList = this.originList
          .filter((item) => item.id !== this.pid)
          .map((item) => item.code)
      }
      existCodeList.includes(value)
        ? callback(new Error('编码被占用了'))
        : callback()
    }
    const validName = (rule, value, callback) => {
      let existNameList = this.originList
        .filter((item) => item.id === this.pid)
        .map((item) => item.name)
      if (this.isEdit) {
        const curId = this.originList.find((item) => item.id === this.pid).pid
        existNameList = this.originList
          .filter((item) => item.pid === curId && item.id !== this.pid)
          .map((item) => item.name)
      }
      console.log('编辑', existNameList)
      existNameList.includes(value)
        ? callback(new Error('名称被占用了'))
        : callback()
    }
    return {
      list: [],
      form: {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门管理者
        introduce: '' // 部门介绍
      },
      rules: {
        name: [
          { required: true, message: '部门名称不能为空', trigger: 'blur' },
          {
            min: 1,
            max: 50,
            message: '部门名称要求1-50位字符',
            trigger: 'blur'
          },
          { validator: validName, trigger: 'blur' }
        ],
        code: [
          { required: true, message: '部门编码不能为空', trigger: 'blur' },
          {
            min: 1,
            max: 50,
            message: '部门编码要求1-50位字符',
            trigger: 'blur'
          },
          { validator: validCode, trigger: 'blur' }
        ],
        manager: [
          { required: true, message: '部门管理者不能为空', trigger: 'blur' }
        ],
        introduce: [
          { required: true, message: '部门介绍不能为空', trigger: 'blur' },
          {
            min: 1,
            max: 300,
            message: '部门介绍要求1-300位字符',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  created() {
    this.loadEmployee()
    if (this.isEdit) {
      this.loadDetail()
    }
  },
  methods: {
    async doAdd() {
      const d = { ...this.form, pid: this.pid }
      await addDepartments(d)
      this.$message.success('添加成功')
      // console.log('新增子部门', res)
    },
    async doEdit() {
      await updateDepartments(this.form)
      this.$message.success('修改成功')
      // console.log('更新部门数据', res)
    },
    // 确定按钮
    hSubmit() {
      // 表单校验
      // 发送请求
      this.$refs.deptForm.validate((valid) => {
        if (valid) {
          this.isEdit ? this.doEdit() : this.doAdd()
          // 通知父组件: 关闭弹层
          this.$emit('success')
        }
      })
    },
    // 获取当前部门的详情信息
    async loadDetail() {
      const res = await getDepartDetail(this.pid)
      // console.log('详情信息', res)
      this.form = res.data
    },
    // 获取员工简单列表
    async loadEmployee() {
      const res = await getEmployee()
      this.list = res.data
    }
  }
}
</script>

<style></style>
